package com.oocl.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.oocl.dao.FoodsDao;
import com.oocl.dao.base.BaseDao;
import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.service.FoodsService;
import com.oocl.util.FileOperatorUtil;
import com.oocl.util.VoUtil;
import com.oocl.vo.FoodsVo;

@Service("foodsService")
public class FoodsServiceImpl implements FoodsService{

	@Autowired
	private BaseDao baseDao;
	@Autowired
	private FoodsDao foodsDao;
	
	@Value("#{configProperties['foodFilePath']}")
	private String foodFilePath;
	@Value("#{configProperties['foodUrlPath']}")
	private String foodUrlPath;
	/*
	 * @Method: addFood
	 * @function: add a Foods 
	 * @param map include food pic file and Foods message
	 * @author garen
	 * @date 2017-08-12
	 */
	@Transactional
	public FoodsVo addFood(Map<String,Object> map) throws IllegalStateException, IOException {
		MultipartFile foodFile = (MultipartFile) map.get("foodFile");
		Foods food = (Foods) map.get("food");
		String mid = (String) map.get("mid");
		if(!foodFile.isEmpty()){
			String foodPicName = FileOperatorUtil.getFileName()+foodFile.getOriginalFilename();//前面加上随机串，避免文件名重复
			String foodFilePosition = foodFilePath+foodPicName;
			FileOperatorUtil.saveFile(foodFile, foodFilePosition);
			String foodUrl = foodUrlPath+foodPicName;
			food.setFimg(foodUrl);
		}
		Merchants merchant = (Merchants) baseDao.find(Merchants.class, mid);
		food.setMerchant(merchant);
		baseDao.persist(food);
		return VoUtil.translate(FoodsVo.class, food);
		
	}
    
	/*
	 * @Method: UpdateFood
	 * @function: update a Foods
	 * @param food a Foods Entity
	 * @author garen
	 * @date 2017-08-12
	 */
	@Transactional
	public FoodsVo updateFood(Map<String,Object> map) throws Exception {
		Foods food = (Foods) map.get("food");
		MultipartFile foodFile = (MultipartFile) map.get("foodFile");
		Foods f = (Foods) baseDao.find(Foods.class, food.getFid());
		if(foodFile!=null&&!foodFile.isEmpty()){
			//delete old file
			if(f.getFimg()!=null||!"".equals(f.getFimg())){
				String[] strs = f.getFimg().split("/");
				String foodFileName = strs[strs.length-1];
				String oldFoodFilePosition = foodFilePath+foodFileName;
				FileOperatorUtil.removeFile(oldFoodFilePosition);
			}
			 //save new file
			String foodPicName = FileOperatorUtil.getFileName()+foodFile.getOriginalFilename();//前面加上随机串，避免文件名重复
			String foodFilePosition = foodFilePath+foodPicName;
			FileOperatorUtil.saveFile(foodFile, foodFilePosition);
			String foodUrl = foodUrlPath+foodPicName;
			food.setFimg(foodUrl);
		}
		 f = (Foods) VoUtil.copyWithoutNull(food, f);
		 baseDao.update(f);
		 return VoUtil.translate(FoodsVo.class, food);
	}

	
	/*
	 * @Method: findFoodById
	 * @function: find a food  by food id
	 * @param m a Foods Entity
	 * @return fvo FoodsVo
	 * @author garen
	 * @date 2017-08-12
	 */
	public FoodsVo findFoodById(String fid) {
		Foods food = (Foods) baseDao.find(Foods.class, fid);
		FoodsVo fvo = null;
		if(food!=null){
		   fvo = VoUtil.translate(FoodsVo.class, food);
		}
		return fvo;
	}
	/*
	 * @Method: findAllByIcnum
	 * @function: find  foods list by icnum
	 * @param icnum -> Merchants icnum
	 * @return voList FoodsVo list
	 * @author garen
	 * @date 2017-08-12
	 */
	public List<FoodsVo> findFoodsByIcnum(String icnum) {
		List<Foods> list = foodsDao.findFoodsByIcnum(icnum);
		List<FoodsVo> voList = new ArrayList<FoodsVo>();
		if(list.size()>0){
			voList = VoUtil.translateList(FoodsVo.class,list);
		}
		return voList;
	}
	/*
	 * @Method: deleteFood
	 * @function: remove a Foods Entity
	 * @param fid ->Foods id
	 * @return fvo-> FoodsVo
	 * @author garen
	 * @date 2017-08-12
	 */
	@Transactional
	public FoodsVo deleteFood(String fid) {
		  Foods food = (Foods) baseDao.find(Foods.class, fid);
		  baseDao.delete(Foods.class, fid);
		  if(food.getFimg()!=null&&!"".equals(food.getFimg())){
			  String[] strs = food.getFimg().split("/");
			  String foodFileName = strs[strs.length-1];
			  String foodFilePosition = foodFilePath+foodFileName;
			  FileOperatorUtil.removeFile(foodFilePosition);
		  }
		  return VoUtil.translate(FoodsVo.class, food);
	}

}
